﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Task.Log;

namespace Task.Util
{
    public class BusinessInfoHelper
    {
        #region 解决发布时含有优质媒体时，前台页面卡住的现象
        //原理：利用生产者消费者模式进行入列出列操作

        public readonly static BusinessInfoHelper Instance = new BusinessInfoHelper();
        private BusinessInfoHelper()
        { }

        private Queue<QueueInfo> ListQueue = new Queue<QueueInfo>();

        public void AddQueue(QueueInfo queueinfo) //入列
        {
            ListQueue.Enqueue(queueinfo);
        }

        public void Start()//启动
        {
            Thread thread = new Thread(threadStart);
            thread.IsBackground = true;
            thread.Start();
        }

        private void threadStart()
        {
            while (true)
            {
                if (ListQueue.Count > 0)
                {
                    try
                    {
                        ScanQueue();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Fatal("threadStart:" + ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                    }
                }
                else
                {
                    //没有任务，休息3秒钟
                    //Thread.Sleep(1000 * 60 * 10);
                }
            }
        }

        //要执行的方法
        private void ScanQueue()
        {
            while (ListQueue.Count > 0)
            {
                try
                {
                    //从队列中取出
                    QueueInfo queueinfo = ListQueue.Dequeue();

                    LogHelper.Fatal("执行任务:" + queueinfo.Times, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }
                catch (Exception ex)
                {
                    LogHelper.Fatal("threadStart:" + ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }
            }
        }


        #endregion
    }

}
